package com.winter.service;

import com.google.common.base.Strings;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.FinCommentInfoMapper;
import com.winter.mapper.FinContentLikeInfoMapper;
import com.winter.mapper.FinFavInfoMapper;
import com.winter.mapper.IAttaMapper;
import com.winter.mapper.IindudyReportPolicyMapper;
import com.winter.model.FinCommentInfo;
import com.winter.model.FinContentLikeInfo;
import com.winter.model.FinFavInfo;
import com.winter.model.IAtta;
import com.winter.model.IindudyReportPolicy;
import com.winter.model.vo.response.IindudyReportPolicyResponseVo;
import com.winter.util.Constants;
import com.winter.util.P2pPubParaConstants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IindudyReportPolicyService {

    @Autowired
    public IindudyReportPolicyMapper iindudyReportPolicyMapper;

    @Autowired
    private FinContentLikeInfoMapper finContentLikeInfoMapper;

    @Autowired
    private FinCommentInfoMapper finCommentInfoMapper;

    @Autowired
    private FinFavInfoMapper finFavInfoMapper;

    @Autowired
    private IAttaMapper iAttaMapper;

    /**
     * 列表（分页）
     */
    public Result informationList(Integer pageSize, Integer pageNumber, String type, String key) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<IindudyReportPolicyResponseVo> iindudyReportPolicyResponseVos = iindudyReportPolicyMapper.informationList(type, key);
        PageInfo<IindudyReportPolicyResponseVo> pageInfo = new PageInfo<>(iindudyReportPolicyResponseVos);
        return Result.success(pageInfo);
    }


    public Result informationDetails(Long id, String userNo) {
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "资讯id不能为空");
        }
        // 修改阅读量 每次+1
        iindudyReportPolicyMapper.updateViewhits(id);
        IindudyReportPolicy iindudyReportPolicy = iindudyReportPolicyMapper.selectByPrimaryKey(id);
        // 获取是否点赞记录
        if (!Strings.isNullOrEmpty(userNo)) {
            List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(id, P2pPubParaConstants.DC_MODULE_TYPE.INFORMATION, iindudyReportPolicy.getTypeflag(), userNo);
            // 大于0 代表当前用户点赞过
            if (isLike.size() > 0) {
                iindudyReportPolicy.setUserIsLike(1);
            } else {
                iindudyReportPolicy.setUserIsLike(0);
            }
            // 大于0 代表当前用户收藏过
            List<FinFavInfo> finFavInfo = finFavInfoMapper.favInfoList(id, Integer.valueOf(P2pPubParaConstants.DC_MODULE_TYPE.INFORMATION), Integer.valueOf(iindudyReportPolicy.getTypeflag()), userNo);
            if (finFavInfo.size() > 0) {
                iindudyReportPolicy.setUserIsFav(1);
            } else {
                iindudyReportPolicy.setUserIsFav(0);
            }
        } else {
            iindudyReportPolicy.setUserIsLike(0);
            iindudyReportPolicy.setUserIsFav(0);
        }
        // 点赞统计
        List<FinContentLikeInfo> isLike = finContentLikeInfoMapper.getIsLike(id, P2pPubParaConstants.DC_MODULE_TYPE.INFORMATION, iindudyReportPolicy.getTypeflag(), null);
        iindudyReportPolicy.setLikeCount(isLike.size());
        // 评论统计
        Integer type = 0;   // 代表评论信息
        List<FinCommentInfo> finCommentInfos = finCommentInfoMapper.commentList(id, Integer.valueOf(P2pPubParaConstants.DC_MODULE_TYPE.INFORMATION), Integer.valueOf(iindudyReportPolicy.getTypeflag()), type);
        iindudyReportPolicy.setCommentCount(finCommentInfos.size());
        // 收藏统计
        List<FinFavInfo> finFavInfo = finFavInfoMapper.favInfoList(id, Integer.valueOf(P2pPubParaConstants.DC_MODULE_TYPE.INFORMATION), Integer.valueOf(iindudyReportPolicy.getTypeflag()), null);
        iindudyReportPolicy.setFavCount(finFavInfo.size());

     //  iindudyReportPolicy.setViewhits(iindudyReportPolicy.getViewhits() + 1);
        // 获取全部附件
        List<IAtta> iAttaImg = iAttaMapper.getIAtta(id, P2pPubParaConstants.AttaType.INDU_TYPE);
        // 获取图片附件
        iindudyReportPolicy.setiAttaImg(iAttaImg);
        List<IAtta> iAttaFile = iAttaMapper.getIAtta(id, P2pPubParaConstants.AttaType.INDU_DOC);
        // 获取文件
        iindudyReportPolicy.setiAttaFile(iAttaFile);
        return Result.success(iindudyReportPolicy);
    }
}
